package com.asj.utils;


import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;

import com.asj.MainActivity;
import com.asj.R;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 通用工具类
 * */
public class CommonsUtils {
    //筛选IMEI号
    public static String screenIMEI(String content){
        //正则匹配，筛选一个连续或单个的数字
        Matcher matcher = Pattern.compile("\\d+").matcher(content);

        // 用于存储所有匹配到的数字
        StringBuilder imei = new StringBuilder();

        // 查找并追加所有匹配的数字
        while (matcher.find()) {
            imei.append(matcher.group());
        }
        return imei.toString();
    }

    //解析获取DeviceCode
    public static String getDeviceCode(String imei) {
        return screenIMEI(imei.split(",")[0]);
    }

    /**
     * 发布通知的方法
     * */
    public static void showNotification(@NonNull Context context, @NonNull String title, @NonNull String content) {
        // 获取系统服务来发布通知
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        // 创建通知渠道（Android 8.0及以上）
        CharSequence channelName = "General";
        String channelId = "general_channel";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
        channel.setDescription("General notifications");
        notificationManager.createNotificationChannel(channel);

        // 构建通知
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "general_channel")
                .setSmallIcon(R.mipmap.ic_launcher) // 设置通知图标
                .setContentTitle(title) // 设置通知标题
                .setContentText(content) // 设置通知内容
                .setPriority(NotificationCompat.PRIORITY_DEFAULT) // 设置通知优先级
                .setAutoCancel(true); // 点击通知后自动取消

        // 如果需要，可以设置点击通知后打开的Intent
        Intent intent = new Intent(context, MainActivity.class); // 根据需要修改目标Activity
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 132, intent, PendingIntent.FLAG_IMMUTABLE);
        builder.setContentIntent(pendingIntent);

        // 发布通知
        int notificationId = (int) System.currentTimeMillis();
        notificationManager.notify(notificationId, builder.build());
    }
}
